//
//  PxHomeViewController.m
//  OilCard
//
//  Created by Xu Yefeng on 14-5-18.
//  Copyright (c) 2014年 Free. All rights reserved.
//

#import "PxUserHomeViewController.h"

@implementation PxUserHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.height = 3*45;
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor colorWithHTMLStr:@"d6d6d6"];
    
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:view];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"enterCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = [UIColor colorWithHTMLStr:@"000000"];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"清除数据";
            break;
        case 1:
//            cell.textLabel.text = @"检查更新";
            cell.textLabel.text = @"关于";
            break;
        case 2:
            cell.textLabel.text = @"退出";
            break;
        case 3:
            cell.textLabel.text = @"退出";
            break;
        case 4:
//            cell.imageView.image = [UIImage imageNamed:@"res/level2icon/信息修改.png"];
//            cell.textLabel.text = @"修改密码";
            cell.textLabel.text = @"寄件须知";
            break;
        case 5:
//            cell.textLabel.text = @"寄件须知";
            cell.textLabel.text = @"保险协议";
            break;
        case 6:
//            cell.textLabel.text = @"保险协议";
            cell.textLabel.text = @"运费时效查询";
            break;
        case 7:
            cell.textLabel.text = @"合作区域";
            break;
        case 8:
            cell.textLabel.text = @"转账信息";
            
//            cell.detailTextLabel.text = @"当前版本为测试版";
//            cell.detailTextLabel.textColor = [UIColor grayColor];
            break;
        case 9:
            cell.textLabel.text = @"关于快客易达";
            break;
        case 10:
            cell.textLabel.text = @"退出登录";
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
//            [self pushViewController:@"PxUserInfoViewController" withIncomingParam:nil];
            [self showConfirmForCache];
            break;
        case 1:
            [self pushViewController:@"PxAboutUsViewController" withIncomingParam:nil];
//            [self checkVersion];
            break;
        case 2:
            [self logout];
            break;
        case 3:
            [self logout];
            break;
        case 4:
//            [self pushViewController:@"PxWebViewController" withIncomingParam:@{@"name":@"寄件须知",
//                                                                                @"url":[NSString stringWithFormat:@"%@/static/word/delivery_notice.html", WEB_HOME]
//                                                                                }];
            break;
        case 5:
//            [self pushViewController:@"PxWebViewController" withIncomingParam:@{@"name":@"保险协议",
//                                                                                @"url":[NSString stringWithFormat:@"%@/static/word/risk_agreement.htm", WEB_HOME]
//                                                                                }];
            break;
        case 6:
//            [self pushViewController:@"PxWebViewController" withIncomingParam:@{@"name":@"运费时效查询",
//                                                                                @"url":[NSString stringWithFormat:@"%@/static/webFreight", WEB_HOME]
//                                                                                }];
            break;
        case 7:
//            [self pushViewController:@"PxWebViewController" withIncomingParam:@{@"name":@"合作区域",
//                                                                                @"url":[NSString stringWithFormat:@"%@/static/webService", WEB_HOME]
//                                                                                }];
            break;
        case 8:
//            [self pushViewController:@"PxWebViewController" withIncomingParam:@{@"name":@"转账信息",
//                                                                                @"url":[NSString stringWithFormat:@"%@/static/webBankTurn", WEB_HOME]
//                                                                                }];
            break;
        case 9:
            [self pushViewController:@"PxAboutUsViewController" withIncomingParam:nil];
            break;
        case 10:
            [self logout];
            break;
        default:
            break;
    }
}

-(void)showConfirmForCache
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"清除现有数据，是否继续"] preferredStyle: UIAlertControllerStyleAlert];
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
        [self removeCache];
    }];
    UIAlertAction *CancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:OKAction];
    [alertController addAction:CancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)removeCache
{
    //===============清除缓存==============
//    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
//    for (NSString *p in files)
//    {
//        NSError *error;
//        NSString *path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
//        if([[NSFileManager defaultManager] fileExistsAtPath:path])
//        {
//            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
//        }
//    }
    [PxGlobal clearAllUserDefaultsData];
    [PxGlobal deleteTabel:TABLE_SCHOOL withID:-1];
    [PxGlobal deleteTabel:TABLE_APARTMENT withID:-1];
    [self showMSG:@"清除完成" withType:0];
}

- (void)checkVersion
{
    PxBaseOilNetCmd *cmd = [PxBaseOilNetCmd cmd];
    cmd.functionName = TRANS_VERSION_CHECK;
    
    [cmd.paramDic addEntriesFromDictionary:@{@"type":@"version_ios"}];
    
    cmd.businessSuccessHandler = ^(NSDictionary* dic)
    {
        NSArray *output = dic[RESULT_DATA];
        float localVersion = [XcodeAppVersion floatValue];
        float minimumVersion = 0.0;
        float lastVersion = 0.0;
        
        for (NSDictionary *data in output) {
            NSString *label = data[@"label"];
            if ([label isEqualToString:@"min_version"]) {
                minimumVersion = [data[@"value"] floatValue];
            }
            if ([label isEqualToString:@"version"]) {
                lastVersion = [data[@"value"] floatValue];
            }
            if ([label isEqualToString:@"url"]) {
                self.updateUrl = data[@"value"];
            }
            if ([data[@"type"] intValue] == 1) {
                self.updateUrl = [data objectForKey:@"url"];
            }
        }
        NSString *versisonContent = [NSString stringWithFormat:@"当前有最新的版本，请更新"];
        
        if (localVersion < minimumVersion) {
            // 强制
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"有新的版本"
                                                            message:versisonContent
                                                           delegate:self
                                                  cancelButtonTitle:@"去更新"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else if (localVersion < lastVersion)
        {
            // 提示
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"有新的版本"
                                                            message:versisonContent
                                                           delegate:self
                                                  cancelButtonTitle:@"去更新"
                                                  otherButtonTitles:@"先不升级", nil];
            
            [alert show];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"已经是最新版本"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            
            [alert show];
        }
        
    };
    
    cmd.businessErrorHandler = ^(NSDictionary* dic)
    {
        [LLAlertService sysAlertWithTip:dic[RESULT_DESC] withTitle:@"错误"];
    };
    
    cmd.errorHandler = ^(NSError* error)
    {
        
    };
    
    
    [LLRestEngine sendCommandRequest:cmd];
}

- (void)logout
{
    [PxNetSession clean];
    [PxGlobal removeData:USER_INFO];
    [self pushRootViewController:@"PxLoginViewController" withIncomingParam:nil];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self setLogoTitleViewWithTitle:@"我的"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
